package controlLayer;


import java.util.List;

import dbLayer.DBConnection;
import dbLayer.DBSalesOrder;
import modelLayer.*;

/**
 * This class is part of WesternStyle sales management system. 
 * It holds information on a given sales order.
 * 
 * @author J. Korsgaard 
 * 		   D. Moelgaard
 * 		   A. Kelemen	
 * 		   T. Valentin
 * @Version 1.1
 * 			
 */
public class OrderCtr {
	
	private SalesOrder  salesOrder;
	private CustomerCtr customerCtr;
	private ProductCtr  productCtr;
	
	
	/**
	 * Constructor of orderCtr 
	 */
	public OrderCtr() {
		
		this.customerCtr = new CustomerCtr();
		this.productCtr = new ProductCtr();
		this.salesOrder = new SalesOrder();
		
	}
	
	/**
	 * Creates a salesOrder object and sets the current date on it.
	 */
	public void createSale() {
		
		this.setSalesOrder(new SalesOrder());
		this.salesOrder.setDate();
		
	}
	
//	/**
//	 * Creates a salesOrder object and adds an orderID
//	 * @return void
//	 */
//	public void createSale() {
//		//int orderID = new DBSalesOrder().getLastID() + 1;
//		this.salesOrder = new SalesOrder(orderID);
//		
//	}
	
	/**
	 * Finds a product and adds it to an OrderLine
	 * 
	 * @param productName
	 * @param amount
	 * @return boolean
	 */
	public OrderLine addProduct(int productID, int amount) {
		
		// finds product and adds it to the salesOrder

		 Product product = this.productCtr.findProductID(productID);
		 
		// creates an orderLine with a product and an amount 
		OrderLine orderLine = new OrderLine();
		orderLine.setAmount(amount);
		orderLine.setProduct(product);
		// adds the orderLine to salesOrder
		if (!this.addOrderLine(orderLine)) {
			return null;
		}
		return orderLine;
		
	}
	
	/**
	 * Adds an orderLine to salesOrder
	 * @param orderLine
	 */
	private boolean addOrderLine(OrderLine orderLine) {
		
		return this.salesOrder.addOrderLine(orderLine);
		
	}
	
	/**
	 * Removes an orderLine from salesOrder
	 * @param orderLine
	 * @return 
	 */
	public boolean removeOrderLine(int productID) {
		
		return this.salesOrder.removeOrderLine(productID);
		
	}
	
	/**
	 * finds a customer and adds it to salesOrder
	 * @param name
	 */
	public void addCustomer(String name) {
		
		Customer customer = this.customerCtr.findCustomerName(name);
		if(customer != null) {			
			this.salesOrder.setCustomer(customer);
		}
		
	}
	
	/**
	 * Inserts the salesOrderObj in DB
	 * 
	 */
	public void endOrder() {
		
		  try {
	            DBConnection.startTransaction();
	            DBSalesOrder sdbObj = new DBSalesOrder();
	            sdbObj.insertOrder(this.salesOrder);
	            DBConnection.commitTransaction();
	      }
	      catch(Exception e) {
	            DBConnection.rollbackTransaction();
	      }

	}

	/**
	 * @return the salesOrder
	 */
	public SalesOrder getSalesOrder() {
		
		return this.salesOrder;
		
	}

	/**
	 * @param salesOrder the salesOrder to set
	 */
	public void setSalesOrder(SalesOrder salesOrder) {
		
		this.salesOrder = salesOrder;
		
	}

	/**
	 * @return the customerCtr
	 */
	public CustomerCtr getCustomerCtr() {
		
		return this.customerCtr;
		
	}

	/**
	 * @param customerCtr the customerCtr to set
	 */
	public void setCustomerCtr(CustomerCtr customerCtr) {
		
		this.customerCtr = customerCtr;
		
	}

	/**
	 * @return the productCtr
	 */
	public ProductCtr getProductCtr() {
		
		return this.productCtr;
		
	}

	/**
	 * @param productCtr the productCtr to set
	 */
	public void setProductCtr(ProductCtr productCtr) {
		
		this.productCtr = productCtr;
		
	}

	public void updateAmount(int amount, int productID) {
		List<OrderLine> ols = this.salesOrder.getOrderLines();
		boolean found = false;
		for (int i = 0; !found && i < ols.size(); i++){
			if (ols.get(i).getProduct().getProductID() == productID){
				ols.get(i).setAmount(amount);
				found = true;
			}
		}
	}
	
}
